const app = getApp()
var CONFIG = require("../../config/config.js");
Page({
    data: {
        widgetTitle: '本日业绩',
        tabArr: {
        curHdIndex: 0, 
        curBdIndex: 0
        },
        widgets_item:false,
        widgets_item1:false,
        widgets_item2: false,
        widgets_item3: false,
        lists:[],
        code: 0,
        msg: '',
        pages: 0,
        page: 0,
        count: 0,
        amount:[0,0],
        nowifilists:[],
        wifilists:[],
        nowifilistsamount:'',
        wifilistsamount: '',
    },

    //收缩展开
    widgetsToggle: function (e) {
        // console.log(e);
        this.setData({
            widgets_item: !this.data.widgets_item,
            widgets_item1:false,
            widgets_item2: false,
            widgets_item3: false,
        });
    },

    widgetsToggle1:function(e){
        this.setData({
            widgets_item:false,
            widgets_item1:!this.data.widgets_item1,
            widgets_item2: false,
            widgets_item3: false,
        })
    }, 

    //收缩展开
    widgetsToggle2: function (e) {
        this.setData({
            widgets_item2: !this.data.widgets_item2,
            widgets_item: false,
            widgets_item1: false,
            widgets_item3: false,
        });
    },

    widgetsToggle3: function (e) {
        this.setData({
            widgets_item: false,
            widgets_item1: false,
            widgets_item2: false,
            widgets_item3: !this.data.widgets_item3,
        })
    }, 

    //tab切换
    tab: function (e) {
        var arr = ['本日业绩', '本周业绩', '本月业绩'];
        //var dataId = e.currentTarget.dataset.id;
        var dataId = e.currentTarget.id;
        // console.log(dataId);
        var obj = {};
        obj.curHdIndex = dataId;
        obj.curBdIndex = dataId;
        this.setData({
            tabArr: obj,
            widgetTitle: arr[dataId],
            widgets_item: false,
            widgets_item1:false,
            widgets_item2: false,
            widgets_item3: false,
            page:0,
            wifilists: [], 
            nowifilists:[],
            lists:[],
        })
        var consultant_id = app.globalData.admin_id;
        this.yejiLoadMore(consultant_id, dataId);
        // 网课已确认
        this.yejiInitWkYqr(consultant_id, dataId);
        // 网课未确认
        this.yejiInitWkWqr(consultant_id, dataId);
    },  

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var consultant_id = app.globalData.admin_id;
        var target = this.data.tabArr.curBdIndex;
        // console.log(target);
        this.yejiLoadMore(consultant_id, target);
        // 网课已确认
        this.yejiInitWkYqr(consultant_id, target);
        // 网课未确认
        this.yejiInitWkWqr(consultant_id, target);
    },

  
    // 用户点击右上角分享

    onShareAppMessage: function () {
        return {
        title: CONFIG.SHARETITLE,
        path: CONFIG.PATHLOGIN
        }
    },


    // 网课已确认
    yejiInitWkYqr(consultant_id, target){
        var that = this;
        var data = {
            consultant_id: consultant_id,
            target: target,
        }
        wx.request({
            url: CONFIG.API_HOST + 'crm/static/yejiInitWkYqr',
            data: data,
            header: {
                'ContentType': 'application/json'
            },
            success: function (res) {
                // console.log(res);
                if (res.data.code == 1 || res.data.code == '1') {
                    var newslist = res.data.list;
                    // console.log(newslist)
                    // console.log(that.data.nowifilists)
                    var lists = that.data.wifilists.concat(newslist);
                    // console.log(lists);
                    // console.log(res.data.amount[0])
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        wifilists: lists,
                        wifilistsamount: res.data.amount[0],
                    })
                    // console.log(that.data.page);
                } else {
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        wifilists: that.data.lists,
                        wifilistsamount: res.data.amount[0],
                    })
                }
            },
            fail: function (res) {
                wx.showModal({
                    title: CONFIG.MODEL_TITLE,
                    showCancel: false,
                    content: CONFIG.REQUEST_FAIL,
                    confirmText: '点击重试',
                    success: function (res) {
                        wx.navigateBack();
                    }
                })
            }
        })
    },

    // 网课未确认
    yejiInitWkWqr(consultant_id, target) {
        var that = this;
        var page = that.data.page;
        var data = {
            consultant_id: consultant_id,
            target: target,
        }
        wx.request({
            url: CONFIG.API_HOST + 'crm/static/yejiInitWkWqr',
            data: data,
            header: {
                'ContentType': 'application/json'
            },
            success: function (res) {
                // console.log(res);
                if (res.data.code == 1 || res.data.code == '1') {
                    var newslist = res.data.list;
                    // console.log(newslist)
                    // console.log(that.data.nowifilists)
                    var lists = that.data.nowifilists.concat(newslist);
                    // console.log(lists);
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        nowifilists: lists,
                        nowifilistsamount: res.data.amount[0],
                    })
                    // console.log(that.data.wifilistsamount)
                } else {
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        nowifilistsamount: res.data.amount[0],
                    })
                }
            },
            fail: function (res) {
                wx.showModal({
                    title: CONFIG.MODEL_TITLE,
                    showCancel: false,
                    content: CONFIG.REQUEST_FAIL,
                    confirmText: '点击重试',
                    success: function (res) {
                        wx.navigateBack();
                    }
                })
            }
        })
    },



    yejiLoadMore: function (consultant_id, target) {
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        let that = this;
        var data = {
            consultant_id: consultant_id,
            target: target,
        }
        wx.request({
            url: CONFIG.API_HOST + 'crm/static/yejiInit',
            data: data,
            header: {
                'ContentType': 'application/json'
            },
            success: function (res) {
                // console.log(res);
                if (res.data.code == 1 || res.data.code == '1') {
                    var newslist = res.data.list;
                    var lists = that.data.lists.concat(newslist);

                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        lists: lists,
                        amount: res.data.amount
                    })
                    // console.log(that.data.lists);
                } else {
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        lists: that.data.lists,
                        amount: res.data.amount
                    })
                }
                wx.hideLoading();
            },
            fail: function (res) {
                wx.hideLoading();
                wx.showModal({
                title: CONFIG.MODEL_TITLE,
                showCancel: false,
                content: CONFIG.REQUEST_FAIL,
                confirmText: '点击重试',
                success: function (res) {
                    wx.navigateBack();
                }
                })
            }
        })
    }
})